package org.example.frw.mybatisplus.metahandler;

import cn.hutool.core.date.DateTime;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

@Component
public class AutoFillMetaObjectHandler implements MetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject) {
        Object createTime = metaObject.getValue("createTime");
        Object updateTime = metaObject.getValue("updateTime");
        Object version = metaObject.getValue("version");

        setFieldValByName("createTime", createTime == null?DateTime.now():createTime, metaObject);
        setFieldValByName("updateTime", updateTime == null?DateTime.now():updateTime, metaObject);
        setFieldValByName("version", version == null? 1L : version , metaObject);
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        setFieldValByName("updateTime", DateTime.now(), metaObject);

    }
}
